<?php
function win_observation_data_handler($request = NULL) {
  if ($request == NULL) {
    if (isset($_POST['request'])) {
      $request = $_POST['request'];
    } elseif (isset($_GET['request'])) {
      $request = $_GET['request'];
    }
  }
  
  $jsonString = urldecode($request);
  $jsonString = str_replace("\\", "", $jsonString);
  $json_request = json_decode($request, true);

  if ($json_request == NULL || count($json_request) == 0) {
    die(json_encode(set_error_arr("Incorrect parameters set.")));
  }

  switch ($json_request['action']) {
    case 'getRegionsForCountry':
      get_regions_for_country($json_request['params']);
      break;
    case 'getCitiesForRegion':
      get_cities_for_region($json_request['params']);
      break;
    case 'getObserverDetails':
      get_observer_details($json_request['params']);
      break;
    case 'saveObservation':
      save_observation($json_request['params']);
      break;
    case 'deleteObservation':
      delete_observation($json_request['params']);
      break;
    case 'uploadImage':
      upload_image($json_request['params']);
      break;
    case 'deleteImage':
      delete_image($json_request['params']);
      break;
    case 'getCOLAutoCompleteValues':
      get_col_autocomplete_values($json_request['params']);
      break;
    case 'performAdvancedSearch':
      require_once('search/win_observation_advanced_search.inc');
      perform_advanced_search($json_request['params']);
      break;
    case 'uploadBulkData':
      require_once('win_observation_bulk_upload.inc');
      upload_bulk_data($json_request['params']);
      break;
    case 'importBulkData':
      require_once('win_observation_bulk_upload.inc');
      import_bulk_data($json_request['params']);
      break;
    case 'submitValidationRequest':
      require_once('validation/win_observation_validator_form.inc');
      submit_validation_request($json_request['params']);
      break;
    case 'getUserValidationExpertise':
      require_once('validation/win_observation_validation_page.inc');
      if (isset($json_request['params'])) {
        $params = $json_request['params'];
        if (isset($params['uid'])) {
          $uid = $params['uid'];
        }
      }
      else { // if userid has not been passed, return info for current user.
        global $user;
        $uid = $user->uid;
      }
      get_user_validation_expertise($uid);
    case 'updateValidationRequestStatus':
      require_once('validation/win_observation_validation_page.inc');
      update_validation_request_status($json_request['params']);
      break;
    case 'getCaptchaImage':
      require_once('image_manipulation.inc');
      get_captcha_image('win_captcha_code');
      break;
    case 'getParentEcosystems':
      require_once('win_observation_ecosystems.inc');
      get_parent_ecosystems();
      break;
    case 'getChildEcosystems':
      require_once('win_observation_ecosystems.inc');
      $parent_id = NULL;
      if (isset($json_request['params'])) {
        $params = $json_request['params'];
        if (isset($params['parent_id'])) {
          $parent_id = $params['parent_id'];
        }
      }
      if ($parent_id == NULL) {
        die(json_encode(set_error_arr("Required parameters are not set.")));
      }
      else {
        get_child_ecosystems($parent_id);
      }
      break;
    default:
      die(json_encode(set_error_arr("Invalid request.")));
      break;
  }
  die();
}

